// Tauri 命令接口模块
pub mod commands;
pub mod error;
pub mod types;
pub mod services;
pub mod utils;

// 重新导出主要类型和函数
pub use commands::*;
pub use error::*;
pub use types::*;
pub use services::*;

// 测试模块
#[cfg(test)]
mod tests;







#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub async fn run() {
    use tauri::Manager;
    use services::manager::get_service_manager;

    tauri::Builder::default()
        .plugin(tauri_plugin_log::Builder::default().build())
        .plugin(tauri_plugin_fs::init())
        .setup(|app| {
            // 初始化日志
            tracing_subscriber::fmt::init();

            // 初始化配置管理器
            let config_state = commands::config::ConfigManagerState::new()
                .expect("Failed to initialize configuration manager");
            app.manage(config_state);

            // 初始化服务管理器
            let handle = app.handle().clone();
            tauri::async_runtime::spawn(async move {
                let service_manager = get_service_manager();
                if let Err(e) = service_manager.initialize().await {
                    eprintln!("Failed to initialize service manager: {}", e);
                } else {
                    println!("Service manager initialized successfully");
                }
            });

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            // 文件系统操作命令
            commands::filesystem::open_workspace,
            commands::filesystem::read_file,
            commands::filesystem::write_file,
            commands::filesystem::watch_directory,
            commands::filesystem::create_file,
            commands::filesystem::create_directory,
            commands::filesystem::delete_file_or_directory,
            commands::filesystem::rename_file_or_directory,
            commands::filesystem::read_directory,

            // CodeGraph 操作命令
            commands::codegraph::initialize_codegraph,
            commands::codegraph::get_graph_data,
            commands::codegraph::update_graph_incremental,
            commands::codegraph::query_graph,
            commands::codegraph::get_graph_stats,
            commands::codegraph::export_graph,
            commands::codegraph::cleanup_graph_cache,
            commands::codegraph::rebuild_graph,
            commands::codegraph::get_graph_health,

            // LSP 操作命令 (暂时注释，等待修复)
            // commands::lsp::start_lsp_server,
            // commands::lsp::stop_lsp_server,
            // commands::lsp::restart_lsp_server,
            // commands::lsp::get_all_lsp_servers_status,
            // commands::lsp::get_lsp_server_stats,
            // commands::lsp::discover_lsp_server_for_file,
            // commands::lsp::auto_start_lsp_server_for_file,
            // commands::lsp::lsp_completion,
            // commands::lsp::lsp_hover,
            // commands::lsp::lsp_definition,
            // commands::lsp::lsp_references,
            // commands::lsp::lsp_symbols,
            // commands::lsp::lsp_workspace_symbols,
            // commands::lsp::lsp_format_document,
            // commands::lsp::lsp_diagnostics,
            // commands::lsp::get_lsp_server_status,

            // 工具命令
            commands::utils::greet,
            commands::utils::get_system_info,
            commands::utils::validate_workspace,

            // 配置管理命令
            commands::config::init_config,
            commands::config::get_config,
            commands::config::get_config_value,
            commands::config::set_config_value,
            commands::config::reset_config,
            commands::config::reset_config_section,
            commands::config::export_config,
            commands::config::import_config,
            commands::config::create_config_backup,
            commands::config::list_config_backups,
            commands::config::restore_config_backup,
            commands::config::init_workspace,
            commands::config::get_workspace_state,
            commands::config::save_workspace_state,
            commands::config::subscribe_config_changes,
            commands::config::get_config_paths,
            commands::config::check_platform_compatibility,
            commands::config::apply_platform_adjustments,

            // 新增统一服务命令
            commands::service::get_service_status,
            commands::service::restart_services,
            commands::service::health_check,

            // 解析服务命令
            commands::parser::parse_file,
            commands::parser::parse_files,
            commands::parser::get_supported_languages,

            // 分析服务命令
            commands::analytics::analyze_project_detailed,
            commands::analytics::get_project_stats,
            commands::analytics::get_recent_activity,
            commands::analytics::get_code_graph
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
